package com.rogo.rogobox.views
{
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.views.components.consts.ContentMiddleConst;
	import com.rogo.rogobox.views.components.consts.PageMainConst;
	import com.rogo.rogobox.views.components.pages.PageProfile;
	import com.sina.microblog.data.MicroBlogRelationshipDescriptor;
	import com.sina.microblog.data.MicroBlogUser;
	import com.sina.microblog.data.MicroBlogUsersRelationship;
	
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	public class PageProfileMediator extends Mediator
	{
		public function PageProfileMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'pageProfileMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var currentUser:MicroBlogUser;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function get view():PageProfile
		{
			return viewComponent as PageProfile;
		}
		
		private function updateDisplay():void
		{
			if (currentUser && 
				currentUser.id == App.getInstance().profilePageUser.id)
			{
				return;
			}
			
			currentUser = App.getInstance().profilePageUser;
			var user:MicroBlogUser = App.getInstance().verifyUser;
			
			view.followCtr.visible = false;
			
			if (currentUser.id == user.id)
			{
				view.favorite.enabled = true;
				view.favorite.buttonMode = true;
				if (!view.favorite.hasEventListener(MouseEvent.CLICK))
					view.favorite.addEventListener(MouseEvent.CLICK, favorite_clickHandler);
				view.title.text = '我的资料';
			}
			else
			{
			    view.favorite.enabled = false;
				view.favorite.buttonMode = false;
				if (view.favorite.hasEventListener(MouseEvent.CLICK))
					view.favorite.removeEventListener(MouseEvent.CLICK, favorite_clickHandler);
				view.title.text = currentUser.screenName + '的资料';
			    sendNotification(AppFacade.CHECK_FOLLOWING, currentUser.id);
			}
			
			view.userImg.source = currentUser.profileImageUrl;
			view.userName.text = currentUser.screenName;
			view.follow.text = currentUser.friendsCount.toString();
			view.weibo.text = currentUser.statusesCount.toString();
			view.follower.text = currentUser.followersCount.toString();
			view.favorite.text = currentUser.favouritesCount.toString();
			view.des.text = currentUser.description;
		}
		
		private function updateRelationship(rsd:MicroBlogRelationshipDescriptor):void
		{
			view.followCtr.visible = true;
		    view.followCtr.text = rsd.isFollowedBy ? '取消关注' : '关注';
		}
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			view.weibo.addEventListener(MouseEvent.CLICK, weibo_clickHandler);
			view.followCtr.addEventListener(MouseEvent.CLICK, followCtr_clickHandler);
		    view.follow.addEventListener(MouseEvent.CLICK, follow_clickHandler);
			view.follower.addEventListener(MouseEvent.CLICK, follower_clickHandler);
			view.previous.addEventListener(MouseEvent.CLICK, previous_clickHandler);
			view.userImg.addEventListener(MouseEvent.CLICK, userImg_clickHandler);
			view.userName.addEventListener(MouseEvent.CLICK, userImg_clickHandler);
			
			updateDisplay();
		}
		
		override public function listNotificationInterests():Array
		{
			return [AppFacade.DISPLAY_USER_PROFILE,
				AppFacade.CHECK_FOLLOWING_RESULT,
				AppFacade.CREATE_FOLLOWING_RESULT,
				AppFacade.CANCEL_FOLLOWING_RESULT];
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case AppFacade.DISPLAY_USER_PROFILE:
					updateDisplay();
					break;
				case AppFacade.CHECK_FOLLOWING_RESULT:
					var urs:MicroBlogUsersRelationship = notification.param as MicroBlogUsersRelationship;
					var rsd:MicroBlogRelationshipDescriptor = urs.target;
					updateRelationship(rsd);
					break;
				case AppFacade.CREATE_FOLLOWING_RESULT:
					var careateUser:MicroBlogUser = notification.param as MicroBlogUser;
					if (careateUser.id == currentUser.id)
						view.followCtr.text = '取消关注';
					break;
				case AppFacade.CANCEL_FOLLOWING_RESULT:
					var cancelUser:MicroBlogUser = notification.param as MicroBlogUser;
					if (cancelUser.id == currentUser.id)
						view.followCtr.text = '关注';
					break;
					break;
				default: 
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handler
		//
		//--------------------------------------------------------------------------
		
		private function weibo_clickHandler(e:MouseEvent):void
		{
			App.getInstance().mainPageDisplay = PageMainConst.DISPLAY_MAIN;
			App.getInstance().mainPageUser = currentUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_MAIN);
			sendNotification(AppFacade.DISPLAY_USER_MAIN);
		}
		
		private function followCtr_clickHandler(e:MouseEvent):void
		{
			if (view.followCtr.text == '关注')
			{
				sendNotification(AppFacade.CREATE_FOLLOWING, currentUser.id);
				view.followCtr.text = '取消关注'
			}
			else
			{
				sendNotification(AppFacade.CANCEL_FOLLOWING, currentUser.id);
				view.followCtr.text = '关注'
			}
		}
		
		private function follow_clickHandler(e:MouseEvent):void
		{
			App.getInstance().friendPageIsFollower = false;
			App.getInstance().friendPageUser = currentUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_FRIEND);
			sendNotification(AppFacade.DISPLAY_USER_FRIENDS);
		}
		
		private function follower_clickHandler(e:MouseEvent):void
		{
			App.getInstance().friendPageIsFollower = true;
			App.getInstance().friendPageUser = currentUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_FRIEND);
			sendNotification(AppFacade.DISPLAY_USER_FOLLOWER);
		}
		
		private function previous_clickHandler(e:MouseEvent):void
		{
			sendNotification(AppFacade.BACK_CONTENT_VIEW);
		}
		
		private function favorite_clickHandler(e:MouseEvent):void
		{
			App.getInstance().mainPageDisplay = PageMainConst.DISPAY_FAVOURITE;
			App.getInstance().mainPageUser = App.getInstance().verifyUser;
			sendNotification(AppFacade.CHANGE_CONTENT_VIEW, ContentMiddleConst.VIEW_MAIN);
			sendNotification(AppFacade.DISPLAY_USER_FAVORITE);
		}
		
		private function userImg_clickHandler(e:MouseEvent):void
		{
			navigateToURL(new URLRequest(currentUser.url));
		}
		
	}
}